package com.javaweb.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.javaweb.admin.entity.KDLCustomer;
import com.javaweb.admin.entity.KDLCustomerLog;
import com.javaweb.admin.entity.MemberLevel;
import com.javaweb.admin.mapper.KDLCustomerMapper;
import com.javaweb.admin.query.KDLCustomerQuery;
import com.javaweb.admin.query.MemberLevelQuery;
import com.javaweb.admin.service.IKDLCustomerService;
import com.javaweb.common.common.BaseQuery;
import com.javaweb.common.common.BaseServiceImpl;
import com.javaweb.common.config.DataSourceType;
import com.javaweb.common.config.SpecifyDataSource;
import com.javaweb.common.utils.JsonResult;
import com.javaweb.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class KDLCustomerServiceImpl extends BaseServiceImpl<KDLCustomerMapper, KDLCustomer> implements IKDLCustomerService {

    @Autowired
    private KDLCustomerMapper kdlCustomerMapper;

    @Override
    @SpecifyDataSource(value = DataSourceType.SINESYS)
    public JsonResult getList(BaseQuery query) {
        KDLCustomerQuery customerQuery = (KDLCustomerQuery) query;
        // 查询条件
        QueryWrapper<KDLCustomer> queryWrapper = new QueryWrapper<>();
        // 会员等级名称
        if (!StringUtils.isEmpty(customerQuery.getCustcode())) {
            queryWrapper.and(wq->{ wq.like("customercode", customerQuery.getCustcode()).or().like("customername", customerQuery.getCustcode());});
        }
        if (!StringUtils.isEmpty(customerQuery.getAddress())) {
            queryWrapper.like("deliveryaddress", customerQuery.getAddress());
        }
        if (!StringUtils.isEmpty(customerQuery.getRegion())) {
            queryWrapper.like("region", customerQuery.getRegion());
        }
        if (!StringUtils.isEmpty(customerQuery.getCustomertype())) {
            queryWrapper.eq("customertype", customerQuery.getCustomertype());
        }
        if (!StringUtils.isEmpty(customerQuery.getFcbz())) {
            queryWrapper.eq("fcbz", customerQuery.getFcbz());
        }
        if (!StringUtils.isEmpty(customerQuery.getStatus())) {
            queryWrapper.eq("status", customerQuery.getStatus());
        }

        queryWrapper.eq("mark", 1);
        queryWrapper.orderByDesc("ncts");

        // 查询分页数据
        IPage<KDLCustomer> page = new Page<>(customerQuery.getPage(), customerQuery.getLimit());
        IPage<KDLCustomer> pageData = kdlCustomerMapper.selectPage(page, queryWrapper);
        return JsonResult.success(pageData);
    }

    @Override
    @SpecifyDataSource(value = DataSourceType.SINESYS)
    public List<KDLCustomerLog> getNCCustomerLogList(String customercode) {
        return kdlCustomerMapper.GetNCCustomerLogList(customercode);
    }

    @Override
    @SpecifyDataSource(value = DataSourceType.SINESYS)
    public JsonResult getCustomerList(BaseQuery query) {
        KDLCustomerQuery customerQuery = (KDLCustomerQuery) query;
        IPage<KDLCustomer> page = new Page<>(customerQuery.getPage(), customerQuery.getLimit());
        IPage<KDLCustomer> pageData = kdlCustomerMapper.getCustomerList(page, customerQuery);
        return JsonResult.success(pageData);
    }

    @Override
    @SpecifyDataSource(value = DataSourceType.SINESYS)
    public JsonResult saveCustomer(KDLCustomer entity) {
        if(entity.getId()!=null && entity.getId()>0) {
            return super.edit(entity);
        }
        else
        {
            return super.add(entity);
        }
    }
}
